package com.itjh.controller;

import com.itjh.annotaiton.Log;
import com.itjh.dto.EmpDto;
import com.itjh.pojo.Emp;
import com.itjh.pojo.MyResponseResult;
import com.itjh.pojo.PageBean;
import com.itjh.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 员工管理Controller
 */

@Slf4j
@RestController
@RequestMapping("emps")
public class EmpController {
    @Autowired
    private EmpService empService;

//    //分页查询员工
//    @GetMapping
//    //@RequestParam(defaultValue = "1")默认查询第一页，每页显示10条数据
//    public MyResponseResult<PageBean<Emp>> getByPage(
//            @RequestParam(defaultValue = "1") Integer page,
//            @RequestParam(defaultValue = "10") Integer pageSize,
//            String name, Short gender,
//            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
//    ) {
//        log.info("分页查询员工，参数：{},{},{},{},{},{}", page, pageSize, name, gender, begin, end);
//        PageBean<Emp> empPageBean = empService.getByPage(page, pageSize, name, gender, begin, end);
//        return MyResponseResult.ok(empPageBean);
//    }

    //带条件的分页查询员工
    @GetMapping
    //@RequestParam(defaultValue = "1")默认查询第一页，每页显示10条数据
    public MyResponseResult<PageBean<Emp>> getByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, EmpDto empDto
    ) {
        log.info("带条件的分页查询员工，参数：{},{},{}", page, pageSize, empDto);
        PageBean<Emp> empPageBean = empService.getByPage(page, pageSize,empDto);
        return MyResponseResult.success(empPageBean);
    }

    //批量删除员工
    @Log
    @DeleteMapping("/{ids}")
    public MyResponseResult<Object> deleteByIds(@PathVariable List<Integer> ids) {
        int i = empService.deleteByIds(ids);
        return i >= 1
                ? MyResponseResult.success("删除成功")
                : MyResponseResult.error(0,"删除失败", null);
//                : MyResponseResult.builder().code(0).msg("删除失败").data(null).build();
    }

    //添加员工
    @Log
    @PostMapping
    public MyResponseResult<Object> addEmp(@RequestBody Emp emp){
        int i = empService.insertEmp(emp);
        return i >= 1
                ? MyResponseResult.success("添加成功")
                : MyResponseResult.error(0,"添加失败", null);
    }

    //修改员工信息
    // 1. 查询回显
    @GetMapping("/{id}")
    public MyResponseResult<Emp> getById(@PathVariable Integer id){
        log.info("根据ID查询员工信息，id：{}", id);
        Emp emp = empService.findById(id);
        return emp != null
                ? MyResponseResult.success(emp)
                : MyResponseResult.error(0,"查询失败", null);
    }

    // 2. 修改员工信息
    @Log
    @PutMapping
    public MyResponseResult<Object> updateEmp(@RequestBody Emp emp){
        int i = empService.updateEmp(emp);
        return i >= 1
                ? MyResponseResult.success("修改成功")
                : MyResponseResult.error(0,"修改失败", null);
    }
}
